<?php 
/***
 * 商品表模型
 */
class GoodsModel extends Model{
	
	public $table = 'goods';
	
	//自动验证
	public $validate = array(
		array('gname','nonull','商品名称不能为空',2,3),
		//图片必须必填
		array('picture','nonull','列表图片未上传',2,3),
		//图册必填
		array('pic_goods','nonull','商品图册未上传',2,3)
	);
	
	//自动完成
	public $auto = array(
		//处理类型id:Type_id
		array('Type_tid','getTid','method',2,1),
		//处理添加时间
		array('addtime','time','function',2,3)
		
	);
	//添加类型属性
	public function getTid(){
		$cid = Q('post.category_cid');
		$data = K('category')->where("cid={$cid}")->find();
		$tid = $data['Type_tid'];
		return $tid;
	}
	
	
	public function addGoods(){
		if(!$this->create()) return false;
		//添加商品表放回自增id
		$gid = $this->add();
		//处理缩略图
		//获取大图
		$bigpic = Q('post.pic_goods');
		$img = new image();
		foreach ($bigpic as $v) {
			//缩略图
			$smallpic[] = $img->thumb($v,dirname($v) . '/small_' . basename($v),150,150);
			$middlepic[] = $img->thumb($v,dirname($v) . '/middle_' . basename($v),300,300);
		}
		//组合上传数据
		$dataDetail = array(
			'smallpic' => implode(',', $smallpic),
			'middlepic'=> implode(',', $middlepic),
			'bigpic'   => implode(',', $bigpic),
			'gdetail'  => $_POST['gdetail'],
			'service'  => $_POST['service'],
			'goods_gid'=> $gid
		); 
		//上传商品详情表数据
		K('GoodsDetail')->add($dataDetail);
		
		//处理商品属性
		$attrClass = Q('post.attrClass');
		$specClass = Q('post.specClass');
		//商品属性
		$attr = Q('post.attr');
		if($attr){
			foreach ($attr as $k => $v) {
				$data = array(
					'gvalue'         => $v,
					'type_attr_taid' => $k,
					'goods_gid'      => $gid,
					'class'			 => 1
				);
				K('GoodsAttr')->add($data);
			}
			
		}
		//商品规格  Q不能处理多维数组
		$spec = $_POST['spec'];
		if($spec){
			foreach ($spec as $key => $value) {
				foreach ($value['tavalue'] as $k => $v) {
					$dataSpec = array(
						'gvalue' 		 => $v,
						'type_attr_taid' => $key,
						'goods_gid'		 => $gid,
						'appendprice'	 => $value['add'][$k],
						'class' 		 => 0
					);
					K('GoodsAttr')->add($dataSpec);
				}
			}
			
			
		}
		
		return true;
		
	}
	
	/**
	 * 编辑
	 */
	public function edit($gid){
		//修改商品表
		$this->where("gid={$gid}")->update();
		//修改商品详情表
		//获取大图
		$bigpic = Q('post.pic_goods');
		$img = new image();
		foreach ($bigpic as $v) {
			//缩略图
			$smallpic[] = $img->thumb($v,dirname($v) . '/small_' . basename($v),200,200);
			$middlepic[] = $img->thumb($v,dirname($v) . '/middle_' . basename($v),300,300);
		}
		//组合上传数据
		$dataDetail = array(
			'smallpic' => implode(',', $smallpic),
			'middlepic'=> implode(',', $middlepic),
			'bigpic'   => implode(',', $bigpic),
			'gdetail'  => $_POST['gdetail'],
			'service'  => $_POST['service'],
			'goods_gid'=> $gid
		); 
		//上传商品详情表数据
		K('GoodsDetail')->where("goods_gid={$gid}")->update($dataDetail);
		
		
		
		//修改商品属性表
		//商品属性
		$attr = Q('post.attr');
		if($attr){
			//清除原来的数据，在做添加
			K('GoodsAttr')->where("goods_gid={$gid} AND class=1")->delete();
			foreach ($attr as $k => $v) {
				$data = array(
					'gvalue'         => $v,
					'type_attr_taid' => $k,
					'goods_gid'      => $gid,
					'class'			 => 1
				);
				K('GoodsAttr')->add($data);
			}
			
		}
		//商品规格  Q不能处理多维数组
		$spec = $_POST['spec'];
		if($spec){
			K('GoodsAttr')->where("goods_gid={$gid} AND class=0	")->delete();
			foreach ($spec as $key => $value) {
				foreach ($value['tavalue'] as $k => $v) {
					$dataSpec = array(
						'gvalue' 		 => $v,
						'type_attr_taid' => $key,
						'goods_gid'		 => $gid,
						'appendprice'	 => $value['add'][$k],
						'class' 		 => 0
					);
					K('GoodsAttr')->add($dataSpec);
				}
			}
			
			
		}
		
		//处理货品列表的数据
		K('GoodsList')->where("goods_gid={$gid}")->delete();
		//表库存清零
		$dataStock = array(
			'stock' => 0
		);
		$this->where("gid={$gid}")->update($dataStock);
		
		return true;
		
	}
	
	
	//关联商品和属性表
//	public function getGoodsInfor{
//		
//	}
	
	/**
	 * 处理点赞添加
	 */
	public function updateLike($gid){
		//更新商品表的点赞数
		$like = $this->where("gid={$gid}")->find();
		$like = $like['likes'] + 1;
		$data = array(
			'gid'  => $gid,
			'likes' => $like
		);
		$this->update($data);
		//添加用户-商品间表
		$ugData = array(
			'goods_gid' => $gid,
			'user_uid'  => $_SESSION['uid'] 
		
		);
		K('GoodsUser')->add($ugData);
		
		return $like;
		
	}




	
}























 ?>